using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using net_work.Data.Entity.Cou;
using net_work.Data.Enum.Cou;

namespace net_work.Config.DataBaseConfig.Cou;

/// <summary>
/// 排课实体配置类
/// </summary>
public class ScheduleEntityTypeConfiguration : IEntityTypeConfiguration<Schedule>
{
    public void Configure(EntityTypeBuilder<Schedule> builder)
    {
        builder.ToTable("cou_sche");

        builder.HasKey(s => s.Id);

        // 配置字段名映射
        builder.Property(s => s.Id).HasColumnName("sche_id");

        // 将枚举数组转换为字符串存储
        builder.Property(s => s.ClassNumbers)
            .HasColumnName("sche_class_nums")
            .IsRequired()
            .HasConversion(
                v => string.Join(",", v), // 枚举列表转字符串
                v => v.Split(',', StringSplitOptions.RemoveEmptyEntries)
                    .Select(n => (ClassNumber)Enum.Parse(typeof(ClassNumber), n))
                    .ToList()
            );

        // 将 WeekDay 枚举映射为数字存储
        builder.Property(s => s.WeekDay)
            .HasColumnName("sche_week_day")
            .IsRequired()
            .HasConversion(
                v => (int)v, // 枚举转数字
                v => (WeekDay)v // 数字转枚举
            );

        builder.Property(s => s.TeachId).HasColumnName("teach_id").IsRequired();

        builder.Property(s => s.CreatedBy).HasColumnName("create_by").IsRequired().HasMaxLength(64);
        builder.Property(s => s.CreatedTime).HasColumnName("create_time").IsRequired();
        builder.Property(s => s.UpdatedBy).HasColumnName("update_by").IsRequired().HasMaxLength(64);
        builder.Property(s => s.UpdatedTime).HasColumnName("update_time").IsRequired();
        builder.Property(s => s.Remark).HasColumnName("remark").HasMaxLength(256);
        builder.Property(s => s.DelFlag).HasColumnName("del_flag").HasDefaultValue('0');
    }
}